package com.example.login.Controller;

import com.example.login.Mapper.CartMap;
import com.example.login.Mapper.GoodsMap;
import com.example.login.Mapper.OrderMap;
import com.example.login.Pojo.*;
import com.example.login.Service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class CartController {
    @Autowired
    CartService cartService;
    @Autowired
    GoodsMap goodsMap;
    @Autowired
    CartMap cartMap;
    ArrayList<PCGoods> pcList=new ArrayList<>();
    ArrayList<mp3Goods> mp3List=new ArrayList<>();
    ArrayList<mobileGoods> mobilePhoneList =new ArrayList<>();
    ArrayList<Cart> cartArrayList;
    Map map=new HashMap();
    //加入购物车
    @RequestMapping("/user/cart/{id}/{num}/{sku_code}")
    public String cart(@PathVariable("id") int id, @PathVariable("num") int num,
                       @PathVariable("sku_code") String sku,
                       HttpSession session,
                       Model model) {
        User  user=(User) session.getAttribute("user");
        model.addAttribute("msg","加入成功！");
        int user_id =user.getId();
        String store_name=goodsMap.getStoreNameByID(id);
        Date date = new Date();
        cartService.insert(user_id, id, sku, date.toString(), num, store_name);
        String cid = goodsMap.determine(id);
        String sku_code="a";
        model.addAttribute("sku",sku_code);

        switch (cid) {
            case "MobilePhone":
                List<mobileGoods> mobileGoods = goodsMap.getMobileGoodsById(id);
                model.addAttribute("mobiles", mobileGoods);
                return "商品详情/手机商品详情";
            case "MP3":
                List<mp3Goods> mp3Goods = goodsMap.getMp3GoodsById(id);
                model.addAttribute("mp3s", mp3Goods);
                return "商品详情/MP3商品详情";
            case "PC":
                List<PCGoods> pcGoods = goodsMap.getPCGoodsById(id);
                System.out.println(pcGoods);
                model.addAttribute("pcs", pcGoods);
                return "商品详情/电脑商品详情";
            default:
                return "/error";
        }
    }
    //进入购物车
    @RequestMapping("/user/cart")
        public String getData ( HttpSession session,
                               Model model){
        User  user=(User) session.getAttribute("user");
        int user_id =user.getId();
        cartArrayList=cartMap.getCart(user_id);
        mobilePhoneList.clear();
        mp3List.clear();;
        pcList.clear();
        map.clear();
        for(int i=0;i<cartArrayList.size();i++){
                int id=cartArrayList.get(i).getGoods_id();
                int num=cartArrayList.get(i).getNum();
                String cid=goodsMap.determine(id);
                String sku=cartArrayList.get(i).getSku_code();
                System.out.println(num);
                System.out.println(id);
                System.out.println(sku);
                System.out.println(cid);
                switch (cid){
                    case "MobilePhone":
                        mobilePhoneList.add(goodsMap.getMobileGood(id,sku));
                        map.put(id+" "+sku,num);
                        System.out.println(mobilePhoneList);
                        System.out.println(map);
                        break;
                    case "PC":
                        pcList.add(goodsMap.getPCGood(id,sku));
                        map.put(id+" "+sku,num);
                        break;
                    case "MP3":
                        mp3List.add(goodsMap.getMp3Good(id,sku));
                        map.put(id+" "+sku,num);
                        break;
                }
            }
            //model.addAttribute("NumMap",map)
            model.addAttribute("MobilePhoneList",mobilePhoneList);
            model.addAttribute("NumMap",map);
            model.addAttribute("PCList",pcList);
            model.addAttribute("MP3List",mp3List);
            session.setAttribute("mobile",mobilePhoneList);
            session.setAttribute("map",map);
            session.setAttribute("pc",pcList);
            session.setAttribute("mp3",mp3List);
            return "购物车";
    }

    //购物车结算
    @Autowired
    OrderMap orderMap;
    @RequestMapping("/user/getTotal/{information}/{totalPrice}")
    public  String s(@PathVariable("information") String id,
                     @PathVariable("totalPrice") int account_intotal,
                     HttpSession session,Model model)
    {
        ArrayList<mobileGoods> mobileGoodsList=(ArrayList<mobileGoods>) session.getAttribute("mobile");
        ArrayList<PCGoods> pcGoodsList= (ArrayList<PCGoods>)session.getAttribute("pc");
        ArrayList<mp3Goods> mp3GoodsList =(ArrayList<mp3Goods>) session.getAttribute("mp3");
        HashMap<String, Integer> map = (HashMap<String, Integer>)session.getAttribute("map");
        User user =(User)session.getAttribute("user");
        int user_id =user.getId();
        int address_id=0;
        //记录长度
        String[] temp=id.split("_");//选择的数量
        Date date=new Date();
        int size=mobileGoodsList.size()+mp3GoodsList.size()+pcGoodsList.size();
        int count=0;//删减商品的数量
        int order_id=orderMap.getMaxId()+1;
        for(int i=0;i<size;i++){

            if(i<mobileGoodsList.size()) {
                if(temp[i+count].equals("0")){mobileGoodsList.remove(i);count++;i--;}
                else{
                    orderMap.insert(order_id,mobileGoodsList.get(i).getId(),1,account_intotal,0,account_intotal,date.toString()
                    ,0,user_id,Integer.parseInt(temp[i]),mobileGoodsList.get(i).getSku_code());
                    cartMap.delete(user_id,mobileGoodsList.get(i).getId());
                    map.put(mobileGoodsList.get(i).getId()+" "+mobileGoodsList.get(i).getSku_code(),Integer.parseInt(temp[i+count]));
                }
            }

            else if(i>=mobileGoodsList.size()&&i<pcGoodsList.size()+mobileGoodsList.size()){
                int index = i -mobileGoodsList.size();
                if(temp[i+count].equals("0")){pcGoodsList.remove(index);count++;i--;}
                else{
                    orderMap.insert(order_id,pcGoodsList.get(index).getId(),1,account_intotal,0,account_intotal,date.toString()
                            ,0,user_id,Integer.parseInt(temp[i]),pcGoodsList.get(index).getSku_code());
                    cartMap.delete(user_id,pcGoodsList.get(index).getId());
                    map.put(pcGoodsList.get(index).getId()+" "+pcGoodsList.get(index).getSku_code(),Integer.parseInt(temp[i+count]));
                }
            }
            else  {
                int index =i -mobileGoodsList.size()-pcGoodsList.size();
                if(temp[i+count].equals("0")){mp3GoodsList.remove(index);count++;i--;}
                else{
                    orderMap.insert(order_id,mp3GoodsList.get(index).getId(),1,account_intotal,0,account_intotal,date.toString()
                            ,0,user_id,Integer.parseInt(temp[i]),mp3GoodsList.get(index).getSku_code());
                    cartMap.delete(user_id,mp3GoodsList.get(index).getId());
                    map.put(mp3GoodsList.get(index).getId()+" "+mp3GoodsList.get(index).getSku_code(),Integer.parseInt(temp[i+count]));
                }
            }
            size=mobileGoodsList.size()+mp3GoodsList.size()+pcGoodsList.size();
        }
        System.out.println(map);
        model.addAttribute("MobilePhoneList",mobileGoodsList);
        model.addAttribute("NumMap",map);
        model.addAttribute("PCList",pcGoodsList);
        model.addAttribute("MP3List",mp3GoodsList);
        return"结算/确认订单2";
    }
    @RequestMapping("/user/delete/{information}")
    public  String delete(@PathVariable("information") String id,
                     HttpSession session,Model model)
    {
        ArrayList<mobileGoods> mobileGoodsList=(ArrayList<mobileGoods>) session.getAttribute("mobile");
        ArrayList<PCGoods> pcGoodsList= (ArrayList<PCGoods>)session.getAttribute("pc");
        ArrayList<mp3Goods> mp3GoodsList =(ArrayList<mp3Goods>) session.getAttribute("mp3");
        HashMap<String, Integer> map = (HashMap<String, Integer>)session.getAttribute("map");

        User user =(User)session.getAttribute("user");
        int user_id =user.getId();
        int address_id=0;
        //记录长度
        String[] temp=id.split("_");
        Date date=new Date();
        int size=mobileGoodsList.size()+mp3GoodsList.size()+pcGoodsList.size();
        int count=0;//删减商品的数量
        for(int i=0;i<size;i++){

            if(i<mobileGoodsList.size()) {
                if(temp[i+count].equals("0")){mobileGoodsList.remove(i);count++;i--;}
                else{
                    cartMap.delete(user_id,mobileGoodsList.get(i).getId());
                    map.put(mobileGoodsList.get(i).getId()+" "+mobileGoodsList.get(i).getSku_code(),Integer.parseInt(temp[i+count]));
                }
            }

            else if(i>=mobileGoodsList.size()&&i<pcGoodsList.size()+mobileGoodsList.size()){
                int index = i -mobileGoodsList.size();
                if(temp[i+count].equals("0")){pcGoodsList.remove(index);count++;i--;}
                else{
                    cartMap.delete(user_id,pcGoodsList.get(index).getId());
                    map.put(pcGoodsList.get(index).getId()+" "+pcGoodsList.get(index).getSku_code(),Integer.parseInt(temp[i+count]));
                }
            }
            else  {
                int index =i -mobileGoodsList.size()-pcGoodsList.size();
                if(temp[i+count].equals("0")){mp3GoodsList.remove(index);count++;i--;}
                else{
                    cartMap.delete(user_id,mp3GoodsList.get(index).getId());
                    map.put(mp3GoodsList.get(index).getId()+" "+mp3GoodsList.get(index).getSku_code(),Integer.parseInt(temp[i+count]));
                }
            }
            size=mobileGoodsList.size()+mp3GoodsList.size()+pcGoodsList.size();
        }
        System.out.println(map);

        return"redirect:/user/cart";
    }
}